<?php

/**
 * @author Djordje Zeljic
 * Date: Jun 2, 2010 10:35:03 AM
 */
class cart_m extends Model {

    public $errors = '';
    public $gupdate = TRUE;

    function cart_m () {
        parent::Model();
        $this->load->library('mloader');
    }// construct of cart_m

    function add_item_excursion() {
        if($this->validate_item_excursion()) {
            // code goes here
            $row = $this->mloader->load('eshop/excursions_m','excursionsm')->read($this->input->post('excursion_id'));
            if($this->input->post('adultquantity') && $this->input->post('adultquantity') > 0) {
                $data = array(
                        'id'        =>'ex_adult_'.$row['id'],
                        'qty'       =>$this->input->post('adultquantity'),
                        'price'     =>$row['adultPrice'],
                        'name'      =>$row['title'].' - Odrasli',
                        'options'   =>array('productcategory'=>'excursion','id'=>$row['id'])
                );
                $this->cart->insert($data);
            }

            if($this->input->post('childquantity') && $this->input->post('childquantity') > 0) {
                $data = array(
                        'id'        =>'ex_child_'.$row['id'],
                        'qty'       =>$this->input->post('childquantity'),
                        'price'     =>$row['childPrice'],
                        'name'      =>$row['title'].' - Djeca',
                        'options'   =>array('productcategory'=>'excursion','id'=>$row['id'])
                );
                $this->cart->insert($data);
            }

            // show success message
            echo json_encode(array('success'=>'success'));
        }else {
            echo json_encode(array('success'=>'failed','message'=>$this->errors));
        }
    }

    function validate_item_excursion() {

        if($this->input->post('adultquantity') == 0 && $this->input->post('childquantity') == 0) {
            $this->errors = 'No quantity entered.<br />';
            return FALSE;
        }

        // now check is product exist
        $this->db->where('id',$this->input->post('excursion_id'));
        $query = $this->db->get('excursions',1);
        if($query->num_rows() == 0) {
            $this->errors = 'Product not exist!<br />';
            return FALSE;
        }

        // now check capacity of excursion by id
        $row = $this->db->where('id',$this->input->post('excursion_id'))
                ->get('excursions')->row_array();
        $cap_sum = $this->input->post('adultquantity') + $this->input->post('childquantity');
        if($cap_sum > $row['capacity']) {
            $this->errors = 'No seats.<br />';
            return FALSE;
        }

        return TRUE;
    }

    /*
     *          TOUR ADD ITEM
    */

    function add_item_tour() {
        if($this->validate_item_tour()) {
            $tour = $this->mloader->load('eshop/tours_m','toursm')->read($this->input->post('tour_id'));
            foreach($this->mloader->load('eshop/toursroomtype_m','roomtypem')->readByTourId($this->input->post('tour_id')) as $rtype) {
                if($_POST['rtqty'][$rtype['id']] && $_POST['rtqty'][$rtype['id']] > 0) {
                    $data = array(
                            'id'        =>'tour_'.$tour['id'].'_'.$rtype['id'],
                            'qty'       =>$_POST['rtqty'][$rtype['id']],
                            'price'     =>$rtype['price'],
                            'name'      =>$tour['title'].' - '.$rtype['type'],
                            'options'   =>array('productcategory'=>'tour','id'=>$tour['id'])
                    );
                    $this->cart->insert($data);
                }
            }// foreach
            echo json_encode(array('success'=>'success'));
        }else {
            echo json_encode(array('success'=>'failed','message'=>$this->errors));
        }
    }

    function validate_item_tour() {
        $checknull = FALSE;
        foreach($this->input->post('rtqty') as $vone) {
            if($vone != 0) $checknull = TRUE;
        }
        if($checknull == FALSE) {
            $this->errors = 'No quantity entered!<br />';
            return FALSE;
        }
        // now check is product exist
        $this->db->where('id',$this->input->post('tour_id'));
        $query = $this->db->get('tours',1);
        if($query->num_rows() == 0) {
            $this->errors = 'Product not exist!<br />';
            return FALSE;
        }

        // now check capacity of excursion by id
        $cap_sum = 0;
        $row = $this->db->where('id',$this->input->post('tour_id'))
                ->get('tours')->row_array();
        foreach($this->input->post('rtqty') as $vone) {
            $cap_sum += $vone;
        }

        if($cap_sum > $row['capacity']) {
            $this->errors = 'No seats.<br />';
            return FALSE;
        }

        return TRUE;
    }

    function update() {
        $total = $this->cart->total_items();

        $item = $this->input->post('rowid');
        $qty = $this->input->post('qty');

        $alldata = array();
        $allupdatedata = array();
        for($i = 0; $i < $total; $i++) {
            if(isset($item[$i]) && isset($qty[$i])) {
                $data = array(
                        'rowid' => $item[$i],
                        'qty'   => $qty[$i]
                );
                $isok = FALSE;
                $options = $this->cart->product_options($item[$i]);
                if($options['productcategory'] == 'excursion') {
                    $isok = $this->update_valid_excursion($options['id'], $data['qty']);
                }

                if($options['productcategory'] == 'tour') {
                    $isok = $this->update_valid_tour($options['id'], $data['qty']);
                }

                // if valid form and capacity, update cart
                if($isok == TRUE) {
                    $this->cart->update($data);
                }else {
                    $this->gupdate = FALSE;
                    $alldata['message'] = $this->errors;
                }
            }
        }

        if($this->gupdate == TRUE){
            $alldata['success'] = 'success';
        }else{
            $alldata['success'] = 'failed';
        }

        $alldata['tbody'] = $this->load->view('eshop/cart/live_cart_v',NULL,true);
        echo json_encode($alldata);
    }

    function update_valid_excursion($id, $qty) {
        $excursion = $this->mloader->load('eshop/excursions_m','exm')->read($id);
        if($excursion['capacity'] < $qty) {
            $this->errors .= 'No seats on '.$excursion['title'].' excursion!'."\n";
            return FALSE;
        }
        return TRUE;
    }

    function update_valid_tour($id, $qty){
        $tour = $this->mloader->load('eshop/tours_m','tourm')->read($id);
        if($tour['capacity'] < $qty) {
            $this->errors .= 'No seats on '.$tour['title'].' tour!'."\n";
            return FALSE;
        }
        return TRUE;
    }

    function deleteItem() {
        $data = array(
                'rowid'=>$this->input->post('rowid'),
                'qty'=>0
        );
        $this->cart->update($data);
        echo json_encode(array('success'=>'success'));
    }

}
?>